package stack;

import linearList.LinkedList;

/**
 * 链表实现栈操作，入栈、出栈采用头插入、头删除
 *
 * @param <T>
 */
public class LinkedStack<T> implements Stack<T> {
    private LinkedList<T> list;

    public LinkedStack() {
        this.list = new LinkedList<>();
    }

    @Override
    public String toString() {
        return this.getClass().getName()+" "+this.list.toString();
    }

    @Override
    public boolean isEmpty() {
        return this.list.isEmpty();
    }

    @Override
    public void push(T x) {
        this.list.insert(0,x);
    }

    @Override
    public T peek() {
        return this.list.get(0);
    }

    @Override
    public T pop() {
        return this.list.remove(0);
    }

    public static void main(String args[]) {
        char[] array = {'w', 'h', 'b', 'y', 't'};
        LinkedStack<Character> stack2 = new LinkedStack<Character>();

        System.out.print("LinkedStack Push: ");
        for (char ch : array) {
            stack2.push(ch);
            System.out.print(ch + "  ");
        }
        System.out.println();

        System.out.print("LinkedStack Pop: ");
//        全部出栈
        while (!stack2.isEmpty())
        {
            System.out.print(stack2.pop().toString() + "  ");
        }
        System.out.println();
    }
}